package com.zdft.promotion.promotion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdft.promotion.common.util.RestfulResult;
import com.zdft.promotion.promotion.bean.PromotionRangeBean;
import com.zdft.promotion.promotion.model.PromotionFilterModel;
import com.zdft.promotion.promotion.service.api.IPromotionRangeService;
import com.zdft.promotion.promotion.vo.PromotionRangeResponseVO;
import com.zdft.promotion.promotion.vo.PromotionRangeVO;
import com.zdft.promotion.sso.util.AuthenticationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * ${comments}
 *
 * @author wangtao
 * @email 
 * @date 2020-02-24 16:46:46
 */
@Api(description = "促销范围控制器")
@RestController
@RequestMapping("promotion/range")
public class PromotionRangeController {
    @Autowired
    private IPromotionRangeService promotionRangeService;

    @ApiOperation("通过主键查询")
    @GetMapping("/findById")
    public PromotionRangeBean findById(@RequestParam Long id){
        return promotionRangeService.getById(id);
    }

    @ApiOperation("查询当前促销流程下的促销范围")
    @GetMapping("/findAll")
    public PromotionRangeResponseVO findAll(@RequestBody @Valid PromotionFilterModel model){
        QueryWrapper<PromotionRangeBean> query = new QueryWrapper<>();
        query.lambda().eq(PromotionRangeBean::getPromotionRulesId, model.getId());
        Page<PromotionRangeBean> page = new Page(model.getCurrent(), model.getSize());
        IPage<PromotionRangeBean> result = promotionRangeService.page(page, query);
        return new PromotionRangeResponseVO(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords());
    }

    @ApiOperation("保存(支持批量)")
    @PostMapping("/save")
    public RestfulResult save(@RequestBody List<PromotionRangeVO> voList){
        List<PromotionRangeBean> beans = new ArrayList<>();
        for (PromotionRangeVO vo : voList) {
            PromotionRangeBean bean = new PromotionRangeBean();
            BeanUtils.copyProperties(vo, bean);
            bean.setCreateTime(new Date());
            bean.setCreateUser(AuthenticationUtil.get().getUserBase().getEmpCode());
            beans.add(bean);
        }
        promotionRangeService.saveBatch(beans);
        return new RestfulResult();
    }

    @ApiOperation("修改")
    @PostMapping("/modify")
    public RestfulResult modify(@RequestBody PromotionRangeVO vo){
        PromotionRangeBean bean = new PromotionRangeBean();
        BeanUtils.copyProperties(vo, bean);
        bean.setUpdateTime(new Date());
        bean.setUpdateUser(AuthenticationUtil.get().getUserBase().getEmpCode());
        promotionRangeService.updateById(bean);
        return new RestfulResult();
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public RestfulResult delete(@RequestParam @NotNull(message = "要删除的主键不能为空") Long id){
        promotionRangeService.removeById(id);
        return new RestfulResult();
    }

}
